// waveeditor.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "waveeditor.h"

#include "MainFrm.h"
#include "ChildFrm.h"
#include "ChildFrameDebug.h"
#include "mygraphframe.h"
#include "MyChidFrameEdit.h"
#include "waveeditorDoc.h"
#include "waveeditorView.h"
#include "outputlist.h"
#include "editscriptview.h"
#include "mygraphview.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CWaveeditorApp

BEGIN_MESSAGE_MAP(ZWaveeditorApp, CWinApp)
	//{{AFX_MSG_MAP(CWaveeditorApp)
	ON_COMMAND(ID_APP_ABOUT			, OnAppAbout)
	ON_COMMAND(ID_FILE_NEW_WAVE		, OnFileNewWave)
	ON_COMMAND(ID_FILE_OPEN			, OnFileOpen)
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW			, CWinApp::OnFileNew)
	//ON_COMMAND(ID_FILE_OPEN			, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP	, CWinApp::OnFilePrintSetup)
	ON_COMMAND(ID_RUNCOMPRESSOR		, OnRuncompressor)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWaveeditorApp construction

ZWaveeditorApp::ZWaveeditorApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CWaveeditorApp object

ZWaveeditorApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CWaveeditorApp initialization

BOOL ZWaveeditorApp::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	SetRegistryKey(_T("Zoran\\Coach\\Wave Editor ver 1"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	if(LoadiniFile() < 0)
	{
		AfxMessageBox("Cannot find < ZWaveeditor.ini >file :: closing programm");
		exit(-1);
	}
	
	pTemplateTree = new CMultiDocTemplate(
		IDR_WAVEEDTYPE,
		RUNTIME_CLASS(ZWaveeditorDoc),
		RUNTIME_CLASS(ChildTreeFrame), // custom MDI child frame
		RUNTIME_CLASS(MainTreeView));
	
	pTemplateList = new CMultiDocTemplate(
		IDR_WAVEEDTYPE,
		RUNTIME_CLASS(ZWaveeditorDoc),
		RUNTIME_CLASS(ChildDebugFrame), // custom MDI child frame
		RUNTIME_CLASS(DebugViewList));

	pTemplateEdit = new CMultiDocTemplate(
		IDR_WAVEEDTYPE,
		RUNTIME_CLASS(ZWaveeditorDoc),
		RUNTIME_CLASS(ChidEditScriptFrame), // custom MDI child frame
		RUNTIME_CLASS(EditScriptView));

	
	pTemplateGraph= new CMultiDocTemplate(
		IDR_WAVEEDTYPE,
		RUNTIME_CLASS(ZWaveeditorDoc),
		RUNTIME_CLASS(ChildGraphFrame), // custom MDI child frame
		RUNTIME_CLASS(GraphView));

	
	// create main MDI Frame window
	ParentMainFrame* pMainFrame = new ParentMainFrame;
	if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
		return FALSE;
	m_pMainWnd = pMainFrame;
	m_pMainWnd->DragAcceptFiles();

	pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);
	AddDocTemplate(pTemplateList);
	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);
	if(cmdInfo.m_strFileName != "")
	{
		OnFileOpen(cmdInfo.m_strFileName);
//		cmdInfo.m_nShellCommand
		if (!ProcessShellCommand(cmdInfo))
		{
			return FALSE;
		}
	}
// Dispatch commands specified on the command line

	// The main window has been initialized, so show and update it.

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
		// No message handlers
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnDtnDatetimechangeDatetimepicker1(NMHDR *pNMHDR, LRESULT *pResult);
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void ZWaveeditorApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CWaveeditorApp message handlers


void ZWaveeditorApp::OnFileNewWave() 
{
	// TODO: Add your command handler code here
	CloseAllDocuments( false );

	ParentMainFrame* mainwnd = (ParentMainFrame*)m_pMainWnd;

	CWinApp::OnFileNew();

	POSITION FirstDocPosition = pTemplateList->GetFirstDocPosition();
	CDocument *pDoc = (ZWaveeditorDoc*)pTemplateList->GetNextDoc( FirstDocPosition );
	POSITION FirstViewPosition = pDoc->GetFirstViewPosition();
	CView* pView = (CView*)pDoc->GetNextView( FirstViewPosition );
	
	mainwnd->pDebugFrame = (ChildDebugFrame*)pView->GetParentFrame();

	mainwnd->OnTreeWindow();
}

void ZWaveeditorApp::OnFileOpen() 
{
	// TODO: Add your command handler code here
//	CWinApp::OnFileOpen();
	CFileDialog file(TRUE,"sow",DocumentDirectory+"\\*.sow");
	if (file.DoModal() == IDOK)	
	{
		OnFileOpen( file.GetPathName() );
	}
}





int ZWaveeditorApp::LoadiniFile()
{
	FILE *pFile	=	fopen("ZWaveeditor.ini","r");
	if(pFile	==	NULL)
	{
		AfxMessageBox("Cannot find ZWaveeditor.ini file");
		return -1;
	}
	char _line[250];
	while(fgets(_line,250,pFile)>0)
	{
		CString fline	=	FormatLine(_line);
		
		if(fline.Find("script",0)>=0)
		{
			int pos	=	fline.Find("=",0);
			if(pos <0)
			{
				return -1;
			}
			ScriptDirectory	=	fline.Mid(pos+1);
			continue;
		}
		
		if(fline.Find("documents",0)>=0)
		{
			int pos	=	fline.Find("=",0);
			if(pos <0)
			{
				return -1;
			}
			DocumentDirectory	=	fline.Mid(pos+1);
			continue;
		}
		
		if(fline.Find("output",0)>=0)
		{
			int pos	=	fline.Find("=",0);
			if(pos <0)
			{
				return -1;
			}
			OutputDirectory	=	fline.Mid(pos+1);
			continue;
		}
		
		if(fline.Find("vectors",0)>=0)
		{
			int pos	=	fline.Find("=",0);
			if(pos <0)
			{
				return -1;
			}
			VectorsDirectory	=	fline.Mid(pos+1);
			continue;
		}

		if(fline.Find("configuration",0)>=0)
		{
			int pos	=	fline.Find("=",0);
			if(pos <0)
			{
				return -1;
			}
			ConfigurationDirectory	=	fline.Mid(pos+1);
		}
	}
	return 0;
}

CString ZWaveeditorApp::FormatLine(CString _line)
{
	int pos = _line.Find("\\\\",0);
	if(pos>-1)
	{
		_line.Mid(pos);
	}
	_line.MakeLower();
//	_line.TrimLeft("\t ");
	_line.Remove('\n');
	_line.Remove('\r');
	_line.Remove('\t');
//	_line.Remove(' ');
//	_line.Replace('\t',' ');
	return _line;

}

void CAboutDlg::OnDtnDatetimechangeDatetimepicker1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
	// TODO: Add your control notification handler code here
	*pResult = 0;
}

void ZWaveeditorApp::OnRuncompressor()
{
	// TODO: Add your command handler code here
	runcompressordlg.DoModal();
}

int ZWaveeditorApp::Run()
{
	// TODO: Add your specialized code here and/or call the base class
	return CWinApp::Run();
}

int ZWaveeditorApp::OnFileOpen(CString filename)
{
	CloseAllDocuments( false );
	if(CWinApp::OpenDocumentFile(filename) != NULL)
	{
		ParentMainFrame* mainwnd = (ParentMainFrame*)m_pMainWnd;
		mainwnd->OnTreeWindow();
	}
	return 0;
}

ZWaveeditorApp::~ZWaveeditorApp()
{
	delete pTemplateTree;
	delete pTemplateEdit;
	delete pTemplateGraph;
}
